package com.ruoyi.framework.config;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.ruoyi.framework.provider.CustomDataSourceProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.List;

/**
 * 动态数据源配置
 * 支持从外部服务器获取数据库密码
 */
@Slf4j
@Configuration
public class DynamicDataSourceConfig {


    @Autowired
    private CustomDataSourceProvider customDataSourceProvider;

    @Bean
    @Primary
    public DataSource dataSource() {
        DynamicRoutingDataSource dynamicRoutingDataSource = new DynamicRoutingDataSource(List.of(customDataSourceProvider));
        dynamicRoutingDataSource.setPrimary("master");
        dynamicRoutingDataSource.setStrict(true);
        return dynamicRoutingDataSource;
    }
}
